<script>
import UseCacheFinishStageMap from "@pinias/UseCacheFinishStageMap";
import UseCacheIdStatMap from "@pinias/UseCacheIdStatMap";
import UseCacheMemory from "@pinias/UseCacheMemory";
import UseCacheTaskLog from "@pinias/UseCacheTaskLog";
import UseFieldTree from "@pinias/UseFieldTree";
import UseLifeModel from "@pinias/UseLifeModel";
import UseSetting from "@pinias/UseSetting";
import UseStage from "@pinias/UseStage";
import UseSurfaceSetting from "@pinias/UseSurfaceSetting";
import UseTask from "@pinias/UseTask";
import UseTimeManageList from "@pinias/UseTimeManageList";
import pinia from "@pinias/pinia";
import * as PathUtils from "@utils/pathUtils";
import { useMessage } from "naive-ui";
import { watch } from "vue";

// git commit 计时、
const message = useMessage();
function foo() {
  if (getSetting().git.length > 0 && getSetting().push2RemoteMin !== undefined) {
    commitGitProject();
    message.info("推送到远程");
  }
}
// pinia获取setting，并且进行fresh
const { freshSetting, saveSetting, fresh, save2File, getSetting, getFileList, deleteFileList, commitGitProject } =
  UseSetting(pinia);
await freshSetting();
setInterval(foo, 1000 * 60 * getSetting().push2RemoteMin);
const setting = getSetting();
watch(setting, async () => {
  await saveSetting();
  // 更新每个文件夹 或者重启
  location.reload();
});
// surfaceSetting
const { getSurfaceSetting, setSurfaceSetting } = UseSurfaceSetting(pinia);
const surfaceSetting = getSurfaceSetting();
setSurfaceSetting(await fresh(PathUtils.surfaceSettingPath, getSurfaceSetting()));
watch(surfaceSetting, async () => {
  if (surfaceSetting.toRemoveMediaPath.length > 0) {
    await deleteFileList(surfaceSetting.toRemoveMediaPath);
    surfaceSetting.toRemoveMediaPath = [];
  }
  await save2File(PathUtils.surfaceSettingPath, getSurfaceSetting());
});
// lifeModel.ts
const { setLifeModel, getLifeModel } = UseLifeModel(pinia);
const lifeModel = getLifeModel();
setLifeModel(await fresh(PathUtils.lifeModelPath, getLifeModel()));
watch(lifeModel, async () => {
  await save2File(PathUtils.lifeModelPath, getLifeModel());
});
// timeManage
const { setTimeManageList, getTimeManageList } = UseTimeManageList(pinia);
const timeManageList = getTimeManageList();
setTimeManageList(await fresh(PathUtils.timeManagePath, getTimeManageList()));
watch(timeManageList, async () => {
  await save2File(PathUtils.timeManagePath, getTimeManageList());
});
// fieldTree
const { setFieldTree, getFieldTree } = UseFieldTree(pinia);
const fieldTree = getFieldTree();
setFieldTree(await fresh(PathUtils.fieldTreePath, getFieldTree()));
watch(fieldTree, async () => {
  await save2File(PathUtils.fieldTreePath, getFieldTree());
});
// stage
const { setStageList, getStageList } = UseStage(pinia);
const stageList = getStageList();
setStageList(await fresh(PathUtils.stagePath, getStageList()));
watch(stageList, async () => {
  await save2File(PathUtils.stagePath, getStageList());
});
// task
const { getTaskList, setTaskList } = UseTask(pinia);
const taskList = getTaskList();
setTaskList(await fresh(PathUtils.taskPath, getTaskList()));
watch(taskList, async () => {
  await save2File(PathUtils.taskPath, getTaskList());
});
// taskLog
const { getCacheTaskLogMap, setCacheTaskLogList } = UseCacheTaskLog(pinia);
// 获取文件路径下的所有文件
const cacheTaskLogMap = getCacheTaskLogMap();
setCacheTaskLogList(await getFileList(PathUtils.taskLogPath));
console.log(cacheTaskLogMap);
watch(cacheTaskLogMap, async () => {
  while (cacheTaskLogMap.changeKeys.length !== 0) {
    for (let i = 0; i < cacheTaskLogMap.changeKeys.length; i++) {
      // console.log(cacheTaskLogMap.changeKeys[i])
      const data = cacheTaskLogMap.cacheTaskLogMap.get(cacheTaskLogMap.changeKeys[i]);
      if (data !== undefined) {
        await save2File(PathUtils.taskLogPath + "/" + data.key + ".json", data);
      }
    }
    cacheTaskLogMap.changeKeys = [];
  }
});
// stat
const { getCacheIdStatMap, setCacheIdStatList } = UseCacheIdStatMap(pinia);
// 获取文件路径下的所有文件
const cacheIdStatMap = getCacheIdStatMap();
setCacheIdStatList(await getFileList(PathUtils.statisticPath));
console.log(cacheIdStatMap);
watch(cacheIdStatMap, async () => {
  while (cacheIdStatMap.changeKeys.length !== 0) {
    // console.log(cacheIdStatMap.changeKeys)
    for (let i = 0; i < cacheIdStatMap.changeKeys.length; i++) {
      const data = cacheIdStatMap.IdStatMap.get(cacheIdStatMap.changeKeys[i]);
      // console.log(cacheIdStatMap.changeKeys[i], data);
      if (data !== undefined) {
        await save2File(PathUtils.statisticPath + "/" + data.id + ".json", data);
      }
    }
    cacheIdStatMap.changeKeys = [];
  }
});
// memory
const { getCacheMemory, setCacheMemoryFileList } = UseCacheMemory(pinia);
// 获取文件路径下的所有文件
const cacheMemory = getCacheMemory();
setCacheMemoryFileList(await getFileList(PathUtils.memoryPath));
console.log(cacheMemory);
watch(cacheMemory, async () => {
  while (cacheMemory.changeKeys.length !== 0) {
    // console.log(cacheTaskLogMap.changeKeys)
    for (let i = 0; i < cacheMemory.changeKeys.length; i++) {
      // console.log(cacheTaskLogMap.changeKeys[i])
      const data = cacheMemory.cacheMemoryMap.get(cacheMemory.changeKeys[i]);
      if (data !== undefined) {
        await save2File(PathUtils.memoryPath + "/" + data.key + ".json", data);
      }
    }
    cacheMemory.changeKeys = [];
  }
});
// finishStage
const { getCacheFinishStageMap, setCacheFinishStageFileList } = UseCacheFinishStageMap(pinia);
// 获取文件路径下的所有文件
const cacheFinishStageMap = getCacheFinishStageMap();
setCacheFinishStageFileList(await getFileList(PathUtils.finishStagePath));
console.log(cacheFinishStageMap);
watch(cacheFinishStageMap, async () => {
  while (cacheFinishStageMap.changeKeys.length !== 0) {
    // console.log(cacheTaskLogMap.changeKeys)
    for (let i = 0; i < cacheFinishStageMap.changeKeys.length; i++) {
      const data = cacheFinishStageMap.cacheFinishStageMap.get(cacheFinishStageMap.changeKeys[i]);
      console.log(cacheFinishStageMap.changeKeys[i], data);
      if (data !== undefined) {
        await save2File(PathUtils.finishStagePath + "/" + data.id + ".json", data);
      }
    }
    cacheFinishStageMap.changeKeys = [];
  }
});
</script>
<script setup>
// import SettingEditDrawer from "./setting/SettingEditDrawer.vue";
import MenuRadio from "./components/MenuRadio.vue";
// import HelloWorld from './components/HelloWorld.vue';
// const editSetting = () => (editSettings.flag = true);
// const editSettings = reactive({
//   flag: false,
//   hello: "banana",
// });
</script>

<template>
  <n-message-provider>
    <!--    <n-button @click="editSetting()"> 设置 </n-button>-->
    <MenuRadio></MenuRadio>
    <!--    <SettingEditDrawer v-model="editSettings"></SettingEditDrawer>-->
  </n-message-provider>
</template>

<style scoped>
.logo {
  height: 6em;
  padding: 1.5em;
  will-change: filter;
  transition: filter 300ms;
}
.logo:hover {
  filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.vue:hover {
  filter: drop-shadow(0 0 2em #42b883aa);
}
</style>
